package com.junjie.web.action.cart;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import com.junjie.dao.impl.BookDaoImpl;
import com.junjie.dao.impl.UserDaoImpl;
import com.junjie.entity.Book;
import com.junjie.entity.Cart;
import com.junjie.entity.CartItem;
import com.junjie.entity.User;
import com.junjie.util.CookieUtil;
import com.junjie.util.MD5Util;
import com.junjie.web.action.BaseAction;

public class CartAction extends BaseAction{
	private Book book;
	private BookDaoImpl bookDao = new BookDaoImpl();
	private String buyInfo;
	private Cart cart ;
	private double dangSum;//总金额
	private double fixedSum;//市场价总额
	private int id;//区分每个input输入框用的
	
	private int qty;//要修改成的商品数量
	private boolean logined;//记录用户是否登录
	private int proId;//传过来的参数,book.getId()
	private UserDaoImpl dao = new UserDaoImpl();
	private User user;
	private String email;
	private String pwd;
	
	/**
	 * 用户点击购买按钮,
	 * 1.判断用户是否已经登录,如果没有登录,在product.jsp页面弹出登录窗口
	 * 2.登录成功够执行下面的操作.
	 * 
	 * 
	 * 将商品添加到购物车中,默认的数量为1
	 * 跳转到购物车页面
	 */
	public String execute(){
		//判断用户是否已经登录
		User u = (User)session.get("loginUser");
		if(u==null){
			//用户没有登录
			logined = false;
		}else{
			logined = true;
		}
		return "tan_login";
	}
	
	/**
	 * 2.登录成功够执行下面的操作.
	 * 
	 * 将商品添加到购物车中,默认的数量为1
	 * 跳转到购物车页面
	 */
	public String addProduct(){
		try {
		//根据url中传入的参数,调用dao查询到当前的图书对象
		book = bookDao.selectById(book.getId());
		//实例化一个商品条目类,用来保存用户购买的商品.
		CartItem item = new CartItem();
		//将book添加到商品条目类中
		item.setBook(book);
		//默认点击购买时,购买的数量为1
		item.setQty(1);
		cart = (Cart)session.get("cart");
		if(cart == null){
			//如果cart为null 说明这是用户第一次点击购买
			//创建一个cart对象,将商品添加到购物车
			cart = new Cart();
			//有可能用户之前已经购买过一些商品，这些商品信息已经以cookie的形式保存在了客户端，需要先恢复
			String content = CookieUtil.findCookie("cart", request);
			cart.load(content);
			session.put("cart", cart);
		}
		boolean flag = cart.add(item);
		dangSum = cart.cost();
		fixedSum = cart.cost("fixed");
		session.put("dangSum", dangSum);
		session.put("fixedSum", fixedSum);
		if(!flag){
			buyInfo = "该商品已经添加到购物车";
		}else{
			buyInfo="购买成功,请继续购物或结算";
			CookieUtil.addCookie("cart", cart.store(), response);
		}
		
	} catch (Exception e) {
		e.printStackTrace();
		return "error";
	}
	return "product" ;
	}
		
	/**
	 * 删除购物车中的商品条目
	 * @return
	 */
	public String deleteCartItem(){
		
		cart = (Cart)session.get("cart");
		cart.delete(book.getId());
		//遍历购物车算出总金额
		dangSum = cart.cost();
		fixedSum = cart.cost("fixed");
		session.put("dangSum", dangSum);
		session.put("fixedSum", fixedSum);
		try {
			CookieUtil.addCookie("cart",cart.store(),response);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "error";
		}
		return toCart();
	}
	/**
	 * 修改购物车中商品数量
	 */
	public String modifyCart(){
		cart = (Cart)session.get("cart");
		if(qty==0){
			cart.delete(id);
		}
		cart.modify(id, qty);
		try {
			CookieUtil.addCookie("cart", cart.store(), response);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return "error";
		}
		//遍历购物车算出总金额
		dangSum = cart.cost();
		fixedSum = cart.cost("fixed");
		session.put("dangSum", dangSum);
		session.put("fixedSum", fixedSum);
		return toCart();
	}
	/**
	 * 跳转到购物车
	 * @return
	 */
	public String toCart(){
		cart = (Cart)session.get("cart");
		if(cart == null){
			//如果cart为null 说明这是用户第一次点击购买
			//创建一个cart对象,将商品添加到购物车
			cart = new Cart();
			//有可能用户之前已经购买过一些商品，这些商品信息已经以cookie的形式保存在了客户端，需要先恢复
			try {
				String content = CookieUtil.findCookie("cart", request);
				cart.load(content);
				session.put("cart", cart);
				dangSum = cart.cost();
				fixedSum = cart.cost("fixed");
				session.put("dangSum", dangSum);
				session.put("fixedSum", fixedSum);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
				return "error";
			}
		}
		return "shopping";
	}
	
	//--------------------------------------------
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public String getBuyInfo() {
		return buyInfo;
	}
	public void setBuyInfo(String buyInfo) {
		this.buyInfo = buyInfo;
	}
	public Cart getCart() {
		return cart;
	}
	public double getDangSum() {
		return dangSum;
	}
	public void setDangSum(double dangSum) {
		this.dangSum = dangSum;
	}
	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public double getFixedSum() {
		return fixedSum;
	}
	public void setFixedSum(double fixedSum) {
		this.fixedSum = fixedSum;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getQty() {
		return qty;
	}
	public void setQty(int qty) {
		this.qty = qty;
	}
	public boolean isLogined() {
		return logined;
	}
	public void setLogined(boolean logined) {
		this.logined = logined;
	}
	public int getProId() {
		return proId;
	}
	public void setProId(int proId) {
		this.proId = proId;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
}
